package com.romotc.qqrobot.bean.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.romotc.joyin.bean.CustGroup;
import com.romotc.joyin.bean.CustGroupService;
import com.romotc.oschina.db.MYSQLEncoder;
import com.romotc.qqrobot.bean.QQUserFriend;
import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;


public class QQUserFriendJsonObject {

	private int retcode;
	private ResultObject result;
	
	/**
	{"retcode":0,"result":
	{"friends":
		[{"flag":0,"uin":4161888596,"categories":0},
			{"flag":0,"uin":4179105702,"categories":0},
			{"flag":0,"uin":2525722880,"categories":0}],
	 "marknames":
	 	[{"uin":4161888596,"markname":"dddd"}],
	 "categories":
	 	[{"index":1,"sort":1,"name":"朋友"},
	 		{"index":2,"sort":2,"name":"家人"},
	 		{"index":3,"sort":3,"name":"同学"},
	 		{"index":4,"sort":4,"name":"sshproxy.tk"}],
	 "vipinfo":
	  [{"vip_level":0,"u":4161888596,"is_vip":0},
	 	 {"vip_level":0,"u":4179105702,"is_vip":0},
	  	{"vip_level":0,"u":2525722880,"is_vip":0}],
	 "info":
	  [{"face":0,"flag":25166338,"nick":"Romotc","uin":4161888596},
	  	{"face":585,"flag":8388608,"nick":"xiao","uin":4179105702},
	  	{"face":0,"flag":2164260864,"nick":"爱名网","uin":2525722880}]}}
					 */
	
	/**
	 * 通过QQUserFriend 创建QQUserFriendJsonObject
	 * @param userFriendList
	 * @return
	 */
	public static QQUserFriendJsonObject QQUserFriendJsonObject(
			List<QQUserFriend> userFriendList) {
		QQUserFriendJsonObject jsonObject = new QQUserFriendJsonObject();
		jsonObject.setRetcode(0);
		
		ResultObject resultObject = new ResultObject();
		jsonObject.setResult(resultObject);
		
		if(userFriendList != null && userFriendList.size() > 0) {
			List<Friends> friends = new ArrayList<Friends>();
			List<Marknames> marknames = new ArrayList<Marknames>();
			//分组 好友分组需要从数据库中获取
			List<Catefories> categories = createCateFories(CustGroupService.getAllGroups());
			List<Vipinfo> vipinfoes = new ArrayList<Vipinfo>();
			List<Info> infoes = new ArrayList<Info>();
			
			Friends friend;
			Marknames markname;
			Vipinfo vipinfo;
			Info info;
			
			for(QQUserFriend quf : userFriendList) {
				
				friend = new Friends();
				//分组 RomUnDo 好友分组需要从数据库中获取
				friend.setCategories(1);
				friend.setFlag(quf.getFlag1());
				friend.setUin(quf.getUin());//对于陌生人客户 该字段设置为-99999
				friends.add(friend);
				
				//备注
				if(quf.getMarkname() != null) {
					markname = new Marknames();
					markname.setMarkname(quf.getMarkname());
					markname.setUin(quf.getUin());
					marknames.add(markname);
				}
				
				//vipInfo
				vipinfo = new Vipinfo();
				vipinfo.setIs_vip(quf.getIs_vip());
				vipinfo.setU(quf.getUin());
				vipinfo.setVip_level(quf.getVip_level());
				vipinfoes.add(vipinfo);
				
				//info
				info = new Info();
				info.setFace(quf.getFace());
				info.setFlag(quf.getFlag2());
				info.setNick(quf.getNick());
				info.setUin(quf.getUin());
				infoes.add(info);
				
			}
			
			resultObject.setFriends(friends.toArray(new Friends[friends.size()]));
			resultObject.setInfo(infoes.toArray(new Info[infoes.size()]));
			resultObject.setVipinfo(vipinfoes.toArray(new Vipinfo[vipinfoes.size()]));
			resultObject.setCategories(categories.toArray(new Catefories[categories.size()]));
			resultObject.setMarknames(marknames.toArray(new Marknames[marknames.size()]));
		}
		
		return jsonObject;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String retStr = "{\"retcode\":0,\"result\":{\"friends\":[{\"flag\":0,\"uin\":4161888596,\"categories\":0},{\"flag\":0,\"uin\":4179105702,\"categories\":0},{\"flag\":0,\"uin\":2525722880,\"categories\":0}],\"marknames\":[{\"uin\":4161888596,\"markname\":\"dddd\"}],\"categories\":[{\"index\":1,\"sort\":1,\"name\":\"朋友\"},{\"index\":2,\"sort\":2,\"name\":\"家人\"},{\"index\":3,\"sort\":3,\"name\":\"同学\"},{\"index\":4,\"sort\":4,\"name\":\"sshproxy.tk\"}],\"vipinfo\":[{\"vip_level\":0,\"u\":4161888596,\"is_vip\":0},{\"vip_level\":0,\"u\":4179105702,\"is_vip\":0},{\"vip_level\":0,\"u\":2525722880,\"is_vip\":0}],\"info\":[{\"face\":0,\"flag\":25166338,\"nick\":\"Romotc\",\"uin\":4161888596},{\"face\":585,\"flag\":8388608,\"nick\":\"xiao\",\"uin\":4179105702},{\"face\":0,\"flag\":2164260864,\"nick\":\"爱名网\",\"uin\":2525722880}]}}";
		ObjectMapper mapper = new ObjectMapper();
		try {
			QQUserFriendJsonObject quser = mapper.readValue(retStr, QQUserFriendJsonObject.class);
			ResultObject result = quser.getResult();
			System.out.println(quser.getResult().getFriends()[0].getUin());
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public int getRetcode() {
		return retcode;
	}
	public void setRetcode(int retcode) {
		this.retcode = retcode;
	}
	public ResultObject getResult() {
		return result;
	}
	public void setResult(ResultObject result) {
		this.result = result;
	}
	
	static class Friends {
		
		public Friends() {
			
		}
	
		private int flag;
		private long uin;
		private int categories;
		public int getFlag() {
			return flag;
		}
		public void setFlag(int flag) {
			this.flag = flag;
		}
		public long getUin() {
			return uin;
		}
		public void setUin(long uin) {
			this.uin = uin;
		}
		public int getCategories() {
			return categories;
		}
		public void setCategories(int categories) {
			this.categories = categories;
		}
	}
	
	static class Marknames {
		
		public Marknames() {
			
		}
		private long uin;
		private String markname;
		public long getUin() {
			return uin;
		}
		public void setUin(long uin) {
			this.uin = uin;
		}
		public String getMarkname() {
			return markname;
		}
		public void setMarkname(String markname) {
			this.markname = markname;
		}
	}
	static class CateforiesComparator implements Comparator{
		@Override
		public int compare(Object arg0, Object arg1) {
			Catefories config1 = (Catefories)arg0;
			Catefories config2 = (Catefories)arg1;
			return config1.getSort() - config2.getSort();
		}
	}
	
	static class Catefories {
		public Catefories() {
			
		}
		private int index;
		private int sort;
		private String name;
		public int getIndex() {
			return index;
		}
		public void setIndex(int index) {
			this.index = index;
		}
		public int getSort() {
			return sort;
		}
		public void setSort(int sort) {
			this.sort = sort;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
	}
	
	
	static class Vipinfo {
		
		public Vipinfo() {
		}
		private int vip_level;
		private long u;
		private int is_vip;
		public int getVip_level() {
			return vip_level;
		}
		public void setVip_level(int vip_level) {
			this.vip_level = vip_level;
		}
		public long getU() {
			return u;
		}
		public void setU(long u) {
			this.u = u;
		}
		public int getIs_vip() {
			return is_vip;
		}
		public void setIs_vip(int is_vip) {
			this.is_vip = is_vip;
		}
	}
	
	static class Info {
		public Info() {
			super();
			// TODO Auto-generated constructor stub
		}
		private int face;
		private long flag;
		private String nick;
		private long uin;
		public int getFace() {
			return face;
		}
		public void setFace(int face) {
			this.face = face;
		}
		public long getFlag() {
			return flag;
		}
		public void setFlag(long flag) {
			this.flag = flag;
		}
		public String getNick() {
			if(nick != null)
				nick = MYSQLEncoder.encodeField(nick);
			return nick;
		}
		public void setNick(String nick) {
			this.nick = nick;
		}
		public long getUin() {
			return uin;
		}
		public void setUin(long uin) {
			this.uin = uin;
		}
	}
	static class ResultObject {
		
		public ResultObject () {
			
		}
		private Friends[] friends;
		private Marknames[] marknames;
		private Catefories[] categories;
		private Vipinfo[] vipinfo;
		private Info[] info;
		
		
		//modify by romotc:jackson转字符串的时候会获取所有getter 并将其返回的对象转换成字符串
		public Map<Long, Friends> friendsMap() {
			if(this.friends == null || this.friends.length == 0) {
				return null;
			}
			
			HashMap<Long, Friends> friendsMap = new HashMap<Long, Friends>();
			for(int i = 0;i < friends.length;i++) {
				Friends friend = friends[i];
				friendsMap.put(friend.getUin(), friend);
			}
			return friendsMap;
		}
		
		public Map<Long, Marknames> marknamesMap() {
			if(this.marknames == null || this.marknames.length == 0) {
				return null;
			}
			
			HashMap<Long, Marknames> markMap = new HashMap<Long, Marknames>();
			for(int i = 0;i < marknames.length;i++) {
				Marknames mark = marknames[i];
				markMap.put(mark.getUin(), mark);
			}
			
			return markMap;
		}
		
		public Map<Long, Vipinfo> vipinfoMap() {
			if(this.vipinfo == null || this.vipinfo.length == 0) {
				return null;
			}
			
			HashMap<Long, Vipinfo> vipMap = new HashMap<Long, Vipinfo>();
			for(int i = 0;i < vipinfo.length;i++) {
				Vipinfo vip = vipinfo[i];
				vipMap.put(vip.getU(), vip);
			}
			
			return vipMap;
		}
		public Map<Long, Info> infoMap() {
			if(this.info == null || this.info.length == 0) {
				return null;
			}
			
			HashMap<Long, Info> infoMap = new HashMap<Long, Info>();
			for(int i = 0;i < info.length;i++) {
				Info info1 = info[i];
				infoMap.put(info1.getUin(), info1);
			}
			
			return infoMap;
		}		
		public Friends[] getFriends() {
			return friends;
		}
		public void setFriends(Friends[] friends) {
			this.friends = friends;
		}
		public Marknames[] getMarknames() {
			return marknames;
		}
		public void setMarknames(Marknames[] marknames) {
			this.marknames = marknames;
		}
		public Catefories[] getCategories() {
			return categories;
		}
		public void setCategories(Catefories[] categories) {
			this.categories = categories;
		}
		public Vipinfo[] getVipinfo() {
			return vipinfo;
		}
		public void setVipinfo(Vipinfo[] vipinfo) {
			this.vipinfo = vipinfo;
		}
		public Info[] getInfo() {
			return info;
		}
		public void setInfo(Info[] info) {
			this.info = info;
		}
	}
	
	/**
	 * 将数据解析
	 * @return
	 */
	public List<QQUserFriend> toQQUserFriends() {
		
		if(this.getResult() == null || this.getResult().friends == null || this.getResult().friends.length ==0) {
			return null;
		}
		
		List<QQUserFriend> qfrList = new ArrayList<QQUserFriend>();
		Friends[] friends = getResult().friends;
		Map<Long, Marknames> marknames = getResult().marknamesMap();
		Map<Long, Vipinfo> vipinfos = getResult().vipinfoMap();
		Map<Long, Info> infos = getResult().infoMap();
		QQUserFriend qFriend;
		Friends friend;
		for(int i = 0;i < getResult().friends.length;i++) {
			friend = friends[i];

			//romUndo
			qFriend = new QQUserFriend();
			fillQQUserFriend(qFriend, friend);
			
			if(marknames != null)
				fillQQUserFriend(qFriend, marknames.get(friend.getUin()));
			if(vipinfos != null)
				fillQQUserFriend(qFriend, vipinfos.get(qFriend.getUin()));
			
			fillQQUserFriend(qFriend, infos.get(friend.getUin()));
			
			qfrList.add(qFriend);
		}
		
		return qfrList;
	}
	private static void fillQQUserFriend(QQUserFriend friend, Info info) {
		if(friend == null || info == null) {
			return;
		}
		
		friend.setFace(info.getFace());
		friend.setFlag2(info.getFlag());
		friend.setNick(info.getNick());
		
	}
	private static void fillQQUserFriend(QQUserFriend friend, Vipinfo vipinfo) {
		if(friend == null || vipinfo == null) {
			return;
		}
		
		friend.setIs_vip(vipinfo.getIs_vip());
		friend.setVip_level(vipinfo.getVip_level());
	}
	private static void fillQQUserFriend(QQUserFriend friend, Marknames marknames) {
		if(friend == null || marknames == null) {
			return;
		}
	
		friend.setMarkname(marknames.getMarkname());
	}
	private static void fillQQUserFriend(QQUserFriend friend, Friends friend2) {
		if(friend == null || friend2 == null) {
			return;
		}
		
		friend.setCategories(friend2.getCategories());
		friend.setFlag1(friend2.getFlag());
		friend.setUin(friend2.getUin());
	}


	/**
	 * 返回好友分组
	 * @return
	 */
	public List<CustGroup> toCustGroups() {
		if(this.getResult() != null && this.getResult().categories != null 
				&& this.getResult().categories.length > 0) {
			Catefories[] catlogs = this.getResult().categories;
			List<CustGroup> groups = new ArrayList<CustGroup>(catlogs.length);
			CustGroup grp = null;
			for(int i = 0;i < catlogs.length;i++) {
				if(catlogs[i] == null)
					continue;
				
				grp = new CustGroup();
				fillCustGroup(grp, catlogs[i]);
				groups.add(grp);
			}
			
			return groups;
		}
		return null;
	}


	private void fillCustGroup(CustGroup grp, Catefories catefories) {
		if(catefories == null || grp == null)
			return;
		
		grp.setGroupName(catefories.getName());
		grp.setQqGrpIndex(catefories.getIndex());
		grp.setSort(catefories.getSort());
	}
	
	/**
	 * 通过custGroup创建QQ分组
	 * @param allGroups
	 * @return
	 */
	private static List<Catefories> createCateFories(List<CustGroup> allGroups) {
		List<Catefories> catlogs = new ArrayList<Catefories>();
		if(allGroups == null || allGroups.isEmpty())
			return catlogs;
		
		Catefories catlog = null;
		for(CustGroup grp : allGroups) {
			catlog = createCateFories(grp);
			if(catlog != null && catlog.getIndex() != 0)
				catlogs.add(catlog);
		}
		
		Collections.sort(catlogs, new CateforiesComparator());
		return catlogs;
	}


	private static Catefories createCateFories(CustGroup grp) {
		if(grp == null)
			return null;
		
		Catefories catlogo = new Catefories();
		catlogo.index = grp.getQqGrpIndex();
		catlogo.name = grp.getGroupName();
		catlogo.sort = grp.getSort();
		
		return catlogo;
	}
	
	
}
